package com.wing.common.util;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wing.cms.domain.Channel;
import com.wing.cms.domain.Site;
import com.wing.cms.service.ChannelManager;
import com.wing.cms.service.SiteManager;
import com.wing.common.Constants;

/**
 *  General purpose utilities
 *
 */
public class SessionFilter implements Filter{
	
	public SessionFilter() {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
	    FilterChain chain) throws IOException, ServletException {
	   
	   HttpServletRequest req = (HttpServletRequest) request;
	   HttpServletResponse res = (HttpServletResponse) response;
	   //System.out.println("++++++++++++start+++++sessionfilter:"+(new Date().getTime())+"+++++++++"+req.getRequestURI()+"+++++++++++++++++++");
	   
	   
	   if(Constants.urlMap==null){
			urlMap();
		}
		String url = request.getServerName();
		String sitepath = (String)Constants.urlMap.get(url);
		String oldsitepath=(String)req.getSession().getAttribute(Constants.SESSION_SITE_ACCESS_PATH);
		if(sitepath!=null && !sitepath.equals(oldsitepath)){
			if(!"localhost".equals(url) || ("localhost".equals(url) && oldsitepath==null)){
				String id= (String)Constants.urlMap.get(url+"_id");
				String site_name=(String)Constants.urlMap.get(url+"_sitename");
				req.getSession().setAttribute(Constants.SESSION_SITE_ACCESS_PATH, sitepath);
				req.getSession().setAttribute(Constants.SESSION_SITE_ID, id);
				req.getSession().setAttribute(Constants.SESSION_SITE_NAME, site_name);
			}
		}
	    chain.doFilter(req, res);
	    
	    //System.out.println("++++++++++++end+++++sessionfilter:"+(new Date().getTime())+"+++++++++"+req.getRequestURI()+"+++++++++++++++++++");
		   
	}

	public void init(FilterConfig filterconfig) throws ServletException {
	}
	
	private void urlMap(){
		Constants.urlMap = new HashMap();
		System.out.println("+++++++++++++++++++++++++init urlmap++"+new Date()+"++++++++++++++++++++++++");
		SiteManager siteManager = (SiteManager) SpringDI.getBean("siteManager");
		List list = siteManager.findAll();
		for(int i=0;i<list.size();i++){
			Map map = (Map)list.get(i);
			Constants.urlMap.put((String)map.get("site_url"), (String)map.get("site_path"));
			Constants.urlMap.put((String)map.get("site_url")+"_id", (String)map.get("id"));
			Constants.urlMap.put((String)map.get("site_url")+"_status", ((Integer)map.get("status")).toString());
			Constants.urlMap.put((String)map.get("site_url")+"_sitename", (String)map.get("site_name"));
			Constants.urlMap.put((String)map.get("site_url")+"_homeurl", getHomeUrl((String)map.get("site_url")));
		}

		Constants.urlMap.put("demo1.58ybl.com", "demo1");
		Constants.urlMap.put("demo1.58ybl.com_id", "0000000500130c066bb0");
		Constants.urlMap.put("demo1.58ybl.com_status", "0");
		Constants.urlMap.put("demo1.58ybl.com_sitename", "示例1");
		Constants.urlMap.put("demo1.58ybl.com_homeurl", getHomeUrl("demo1.58ybl.com"));
		
		Constants.urlMap.put("demo2.58ybl.com", "demo2");
		Constants.urlMap.put("demo2.58ybl.com_id", "000000031001312661ce70");
		Constants.urlMap.put("demo2.58ybl.com_status", "0");
		Constants.urlMap.put("demo2.58ybl.com_sitename", "示例2");
		Constants.urlMap.put("demo2.58ybl.com_homeurl", getHomeUrl("demo2.58ybl.com"));
		
		Constants.urlMap.put("demo3.58ybl.com", "demo3");
		Constants.urlMap.put("demo3.58ybl.com_id", "00000001001316a2cf533");
		Constants.urlMap.put("demo3.58ybl.com_status", "0");
		Constants.urlMap.put("demo3.58ybl.com_sitename", "示例3");
		Constants.urlMap.put("demo3.58ybl.com_homeurl", getHomeUrl("demo3.58ybl.com"));
		
		Constants.urlMap.put("58ybl.com", "ybl");
		Constants.urlMap.put("58ybl.com_id", "yblsite01");
		Constants.urlMap.put("58ybl.com_status", Site.SITE_STATUS_DIRECT);
		Constants.urlMap.put("58ybl.com_sitename", "雅宝网");
		Constants.urlMap.put("58ybl.com_homeurl", getHomeUrl("58ybl.com"));
		
		Constants.urlMap.put("wingcms.com", "qzw");
		Constants.urlMap.put("wingcms.com_id", "000000020012e664bc4c8");
		Constants.urlMap.put("wingcms.com_status", Site.SITE_STATUS_DIRECT);
		Constants.urlMap.put("wingcms.com_sitename", "企助网");
		Constants.urlMap.put("wingcms.com_homeurl", getHomeUrl("wingcms.com"));
		
		Constants.urlMap.put("127.0.0.1", "qzw");
		Constants.urlMap.put("127.0.0.1_id", "000000020012e664bc4c8");
		Constants.urlMap.put("127.0.0.1_status", Site.SITE_STATUS_DIRECT);
		Constants.urlMap.put("127.0.0.1_sitename", "企助网");
		Constants.urlMap.put("127.0.0.1_homeurl", getHomeUrl("127.0.0.1"));
	}
	private String getHomeUrl(String url){
		ChannelManager channelManager = (ChannelManager) SpringDI.getBean("channelManager");
		String sitepath=(String)Constants.urlMap.get(url);
		String status=(String)Constants.urlMap.get(url+"_status");
		if(Site.SITE_STATUS_DIRECT.equals(sitepath)){
			return "/site/"+sitepath+"/index.jsp";
		}else{
			List list = channelManager.findChannelsBySiteId( (String)Constants.urlMap.get(url+"_id"));
			String redircturl = "/site/"+sitepath+"/index.jsp";
			if(list.size()>0){
				Map map = (Map)list.get(0);
				Channel c = channelManager.findById((String)map.get("id"));
				if(c!=null && c.getIspub()==1){
					redircturl = "/site/"+sitepath+"/"+map.get("id")+".html";
				}else{
					redircturl = "/cms/channel/layout/"+map.get("id");
				}
			}
			return redircturl;
		}
	}
  
}
